<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\saveNewTemplete;
use App\Models\NewArticle;
use App\Models\Dede;

class ReorganizeNewTemplate extends Command {
    
    protected $signature = 'rnt {from} {to}';
//     protected $signature = 'rnt';
    protected $description = 'ReorganizeNewTemplate';
    
    public function __construct() {
        parent::__construct ();
    }
    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function handle() {        
        $from = $this->argument('from');
        $to = $this->argument('to');
        $this->error('开始整理新加盟模板数据');
        $nts = Dede::select('id', 'ppmc', 'xmtj', 'xmjs')->offset($from)->limit($to)->get();
//         $nts = Dede::select('ppmc', 'xmtj', 'xmjs')->get();
        $cnts = $nts->count();
//         $nts = $nts->toArray();
        $i = 1;
        foreach ($nts as $nt) {
            $this->info($i.' / '.$cnts.', '.$nt->ppmc);
            $nt->xmtj = self::formatContent($nt->xmtj, $nt->ppmc);
            $nt->xmjs = self::formatContent($nt->xmjs, $nt->ppmc);
            $nt->save();
            $i++;
            unset($nt);
        }
        $this->error('执行完毕');
    }
    
    public function formatContent($content, $brandName) {
        $format = str_replace($brandName, "{name}", $content);
        return $format;
    }
}
